package me.zhengjie.modules.finace_taxation.rest;

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.annotation.Log;
import me.zhengjie.base.PageInfo;
import me.zhengjie.modules.finace_taxation.domain.FtIndustryDetails;
import me.zhengjie.modules.finace_taxation.domain.FtLiveBroadcast;
import me.zhengjie.modules.finace_taxation.service.FtIndustryDetailsService;
import me.zhengjie.modules.finace_taxation.service.dto.FtIndustryDetailsDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtIndustryDetailsQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author 杨荣森
* @date 2021-06-23
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "industry_details管理")
@RequestMapping("/api/ft/ftIndustryDetails")
public class FtIndustryDetailsController {

    private final FtIndustryDetailsService ftIndustryDetailsService;

    @GetMapping
    @Log("查询行业详情")
    @ApiOperation("查询行业详情")
//    @PreAuthorize("@el.check('ftIndustryDetails:list')")
    public ResponseEntity query(FtIndustryDetailsQueryParam query, Pageable pageable){
        return new ResponseEntity<>(ftIndustryDetailsService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增行业详情")
    @ApiOperation("新增行业详情")
//    @PreAuthorize("@el.check('ftIndustryDetails:add')")
    public ResponseEntity create(@Validated @RequestBody FtIndustryDetailsDto resources){
        resources.setId("");
        return new ResponseEntity<>(ftIndustryDetailsService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改行业详情")
    @ApiOperation("修改行业详情")
//    @PreAuthorize("@el.check('ftIndustryDetails:edit')")
    public ResponseEntity update(@Validated @RequestBody FtIndustryDetailsDto resources){
        ftIndustryDetailsService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除行业详情")
    @ApiOperation("删除行业详情")
//    @PreAuthorize("@el.check('ftIndustryDetails:del')")
    public ResponseEntity delete(@RequestBody Set<String> ids) {
        ftIndustryDetailsService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("/queryIndustryList")
    @Log("查询行业详情")
    @ApiOperation("查询行业详情")
    public ResponseEntity queryIndustryList(){
        return new ResponseEntity<>(ftIndustryDetailsService.queryIndustryList(),HttpStatus.OK);
    }

    @GetMapping("/getById")
    @Log("查询行业详情")
    @ApiOperation("查询行业详情")
//    @PreAuthorize("@el.check('ftMeetings:del')")
    public ResponseEntity getById(@RequestParam(name="id",required=true)  String id) {
        FtIndustryDetails industryDetails = ftIndustryDetailsService.getById(id);
        return new ResponseEntity<>(industryDetails,HttpStatus.OK);
    }

    /**
     * 分页查询行业详情
     * @param map
     * @return
     */
    @PostMapping("/getIndustryByPage")
    @Log("查询行业详情")
    @ApiOperation("查询行业详情")
//    @PreAuthorize("@el.check('ftMeetings:del')")
    public ResponseEntity getIndustryByPage(@RequestBody Map<String,Object> map) {
        PageInfo<FtIndustryDetails> industryDetails = ftIndustryDetailsService.getIndustryByPage(map);
        return new ResponseEntity<>(industryDetails,HttpStatus.OK);
    }

    /**
     * 点击行业动态增加浏览量
     * @param id
     * @return
     */
    @GetMapping("/addBrowseNumber")
    @Log("点击增加浏览量")
    @ApiOperation("点击增加浏览量")
    public JSONObject addBrowseNumber(@RequestParam(name="id",required=true) String id){
        JSONObject resObj = new JSONObject();
        int i = ftIndustryDetailsService.addBrowseNumber(id);
        if (i > 0) {
            resObj.put("message", "添加成功");
            resObj.put("code", 200);
            resObj.put("success", true);
            return resObj;
        }
        resObj.put("message", "添加失败");
        resObj.put("code", 500);
        resObj.put("success", false);
        return resObj;
    }


    @Log("导出行业行情")
    @ApiOperation("导出行业行情")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, FtIndustryDetailsQueryParam query) throws IOException {
        ftIndustryDetailsService.download(ftIndustryDetailsService.queryAll(query), response);
    }


}
